package com.tanhua.dubbo.api.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.mapper.UserInfoMapper;
import com.tanhua.dubbo.pojo.UserInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@DubboService(version = "1.0.0")
public class UserInfoApiImpl implements UserInfoApi {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public Boolean save(UserInfo userInfo) {
        //校验对象中的数据是否符合要求
        if (!ObjectUtil.isAllNotEmpty(userInfo.getUserId(), userInfo.getNickName())) {
            return false;
        }

        userInfo.setAge(20);//默认值
        userInfo.setEdu("本科");//默认值
        userInfo.setIncome("40");//默认值
        userInfo.setIndustry("计算机");//默认值
        userInfo.setMarriage("未婚");//默认值
        userInfo.setTags("单身,本科,年龄相仿");//默认值
        return this.userInfoMapper.insert(userInfo) == 1;
    }

    @Override
    public Boolean update(UserInfo userInfo) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userInfo.getUserId()); //条件
        return this.userInfoMapper.update(userInfo, queryWrapper) == 1;
    }

    @Override
    public UserInfo queryByUserId(Long userId) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId); //条件
        return this.userInfoMapper.selectOne(queryWrapper);
    }

    @Override
    public List<UserInfo> queryByUserIdList(List<Object> userIdList) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("user_id", userIdList); //条件
        return this.userInfoMapper.selectList(queryWrapper);
    }

    @Override
    public Map<Long, UserInfo> queryMapByUserIdList(List<Object> userIdList) {
        List<UserInfo> userInfoList = this.queryByUserIdList(userIdList);
        return userInfoList.stream()
                .collect(Collectors.toMap(UserInfo::getUserId, userInfo -> userInfo));
    }

    @Override
    public List<UserInfo> queryListLikeNickName(List<Object> userIdList, String keyword) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("user_id", userIdList); //条件
        queryWrapper.like("nick_name", keyword); //模糊查询
        return this.userInfoMapper.selectList(queryWrapper);
    }
}
